<?php



/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Education
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */
 
 
 
class Education_Form_Create extends Engine_Form
{
  public $_error = array();

  public function init()
  {
    $user = Engine_Api::_()->user()->getViewer();
    $user_level = Engine_Api::_()->user()->getViewer()->level_id;    
    
    $this->setTitle('Add New Education')
      ->setDescription('Compose your education below, then click "Post Education" to add your education.')
      ->setAttrib('name', 'educations_create');
  
      
    $this->addElement('Text', 'title', array(
      'label' => 'School Name',
      'description' => 'What school did you go to?',
      'allowEmpty' => false,
      'required' => true,
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
        new Engine_Filter_StringLength(array('max' => '127')),
    )));
    $this->title->getDecorator("Description")->setOption("placement", "append");    
    
    $categories = Engine_Api::_()->education()->getCategories();
    $categories_prepared = Engine_Api::_()->education()->convertCategoriesToArray($categories);
    $categories_prepared = array(""=>"") + $categories_prepared;
    
    // category field
    $this->addElement('Select', 'category_id', array(
          'label' => 'Category',
          'multiOptions' => $categories_prepared,
          'allowEmpty' => false,
          'required' => true,
          'validators' => array(
            array('NotEmpty', true),
          ),
          'filters' => array(
           'Int'
          ),
    ));    
    
    $date = new Zend_Date();
    $year = (int) $date->get(Zend_Date::YEAR);
    $ranges = range($year + 10, 1900);
    $year_ranges = array_combine($ranges, $ranges);
    
    $this->addElement('Select', 'class_year', array(
      'label' => 'Class Year',
      'multiOptions' => array("0"=>"") + $year_ranges
    ));
    
    $this->addElement('Text', 'concentration', array(
      'label' => 'Concentration',
      'description' => 'Ex: Computer Science',
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
        new Engine_Filter_StringLength(array('max' => '127')),
    )));
    $this->concentration->getDecorator("Description")->setOption("placement", "append");    
    
    $this->addElement('Text', 'minor', array(
      'label' => 'Minor',
      'description' => 'Ex: Finance',
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
        new Engine_Filter_StringLength(array('max' => '127')),
    )));    
    $this->minor->getDecorator("Description")->setOption("placement", "append");    
    
    $this->addElement('Text', 'degree', array(
      'label' => 'Degree',
      'description' => 'Ex: AA, BS, MS, MD, or Ph.D',
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
        new Engine_Filter_StringLength(array('max' => '127')),
    )));     
    $this->degree->getDecorator("Description")->setOption("placement", "append");    
    
    // Description
    $this->addElement('Textarea', 'description', array(
      'label' => 'Description',
      'description' => 'Tell us something about your experience or this school.',
      'filters' => array(
        new Engine_Filter_Censor(),
      ),
    ));
    $this->description->getDecorator("Description")->setOption("placement", "append");        

    $this->addElement('File', 'photo', array(
      'label' => 'Photo'
    ));
    $this->photo->addValidator('Extension', false, 'jpg,png,gif,jpeg');    
    
    $this->addElement('Text', 'keywords',array(
      'label'=>'Tags (Keywords)',
      'autocomplete' => 'off',
      'description' => 'Separate tags with commas.',
      'filters' => array(
        new Engine_Filter_Censor(),
      ),
    ));
    $this->keywords->getDecorator("Description")->setOption("placement", "append");    
   
    // Add subforms
    if (!$this->_item){
      $customFields = new Education_Form_Custom_Fields();
    }
    else $customFields = new Education_Form_Custom_Fields(array('item'=>$this->getItem()));
    
    $this->addSubForms(array(
      'customField' => $customFields
    ));
    
    // View
    $availableLabels = array(
      'everyone'              => 'Everyone',
      'registered'            => 'Registered Members',
      'owner_network'         => 'Friends and Networks',
      'owner_member_member'   => 'Friends of Friends',
      'owner_member'          => 'Friends Only',
      'owner'                 => 'Just Me'
    );
    
    
    $options = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('education', $user, 'auth_view');
    $options = array_intersect_key($availableLabels, array_flip($options));

    $this->addElement('Select', 'auth_view', array(
      'label' => 'Privacy',
      'description' => 'Who may see this education?',
      'multiOptions' => $options,
      'value' => 'everyone',
    ));
    $this->auth_view->getDecorator('Description')->setOption('placement', 'prepend');

    $options =(array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('education', $user, 'auth_comment');
    $options = array_intersect_key($availableLabels, array_flip($options));

    // Comment
    $this->addElement('Select', 'auth_comment', array(
      'label' => 'Comment Privacy',
      'description' => 'Who may post comments on this education?',
      'multiOptions' => $options,
      'value' => 'registered',
    ));
    $this->auth_comment->getDecorator('Description')->setOption('placement', 'prepend');

    
    $this->addElement('Checkbox', 'search', array(
      'label' => 'Show this education in search results',
      'value' => 1
    ));
    
    // Buttons
    $this->addElement('Button', 'submit', array(
      'label' => 'Post Education',
      'type' => 'submit',
      'ignore' => true,
      'decorators' => array('ViewHelper')
    ));

    $this->addElement('Cancel', 'cancel', array(
      'label' => 'cancel',
      'link' => true,
      'prependText' => ' or ',
      'decorators' => array(
        'ViewHelper'
      )
    ));
    
    $this->addDisplayGroup(array('submit', 'cancel'), 'buttons');
    $button_group = $this->getDisplayGroup('buttons');
    $button_group->addDecorator('DivDivDivWrapper');    

    
  }
  
  
}